import { GetCartList, DelCart } from "~/api";

export const useContent = () => {
    const list = ref([]);
    const navActive = ref("cart");
    const allChecked = ref([]);
    const keyword = ref("");
    const searchKeyword = ref("");
    const navList = reactive([
        {
            label: "购物车",
            icon: _oss_ + "images/admin/trading-center/icon-cart.png",
            activeIcon:
                _oss_ + "images/admin/trading-center/icon-cart-active.png",
            value: "cart",
        },
    ]);
    const payServiceRef = ref(null);

    const totalPrice = computed(() => {
        if (allChecked.value.length > 0) {
            return list.value
                .filter((item) => allChecked.value.includes(item.id))
                .reduce((total, item) => total + Number(item.price), 0)
                .toFixed(2);
        } else {
            return "0.00";
        }
    });

    const renderList = computed(() => {
        if (searchKeyword.value) {
            return list.value.filter((item) =>
                item.name.includes(searchKeyword.value)
            );
        } else {
            return list.value;
        }
    });

    const GetList = async () => {
        const res = await GetCartList();
        if (res.code == 200) {
            list.value = res.data;
        }
    };

    const onSelect = (id) => {
        if (id === -1) {
            // 点击全选按钮
            if (allChecked.value.length === list.value.length) {
                allChecked.value = [];
            } else {
                allChecked.value = list.value.map((item) => item.id);
            }
        } else {
            // 点击单选按钮
            // 判断是否存在
            if (allChecked.value.includes(id)) {
                allChecked.value = allChecked.value.filter(
                    (item) => item !== id
                );
            } else {
                allChecked.value.push(id);
            }
        }
    };

    const onPreview = async (id) => {
        navigateTo("/materialStore/" + id + ".html", {
            open: true,
        });
    };

    const onSearch = () => {
        searchKeyword.value = keyword.value;
    };

    const onDel = async (id) => {
        const res = await DelCart({
            store_id: id,
        });
        if (res.code == 200) {
            Message().success("删除成功");

            // 不发请求直接删除
            allChecked.value = allChecked.value.filter((item) => item !== id);
            list.value = list.value.filter((item) => item.id !== id);
        }
    };

    const onBuy = () => {
        if (allChecked.value.length == 0) {
            Message().warning("请选择商品");
            return;
        }

        payServiceRef.value.show(
            list.value
                .filter((item) => allChecked.value.includes(item.id))
                .map((item) => item.store_id)
                .join(","),
            true
        );
    };

    return {
        list,
        renderList,
        navActive,
        allChecked,
        navList,
        totalPrice,
        payServiceRef,
        keyword,
        GetList,
        onSelect,
        onDel,
        onPreview,
        onBuy,
        onSearch,
    };
};
